package com.eqdd.yiqidian.ui.mine.pizhun;

import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;

import com.eqdd.yiqidian.NoticeInfoActivityCustom;
import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.RecyclerViewCustom;
import com.eqdd.yiqidian.base.AppConstent;
import com.eqdd.yiqidian.base.Config;
import com.eqdd.yiqidian.bean.rebuild.TiaogangInfoBean;
import com.eqdd.yiqidian.bean.rebuild.TiaogangInfoBean;
import com.eqdd.yiqidian.bean.rebuild.slimedit.SlimEditEnterBean;
import com.eqdd.yiqidian.bean.rebuild.slimedit.SlimEditHintBean;
import com.eqdd.yiqidian.http.DialogCallBack;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.http.HttpResult;
import com.eqdd.yiqidian.ui.base.SlimBaseMultiEditActivity;
import com.eqdd.yiqidian.ui.mine.UserCardActivity;
import com.eqdd.yiqidian.ui.mine.renwu.TextActivity;
import com.eqdd.yiqidian.utils.ToastUtil;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

/**
 * @author吕志豪 .
 * @date 17-12-7  下午4:43.
 * Github :https://github.com/lvzhihao100
 * E-Mail：1030753080@qq.com
 * 简书 :http://www.jianshu.com/u/6e525b929aac
 */

public class TiaogangInfoActivity extends SlimBaseMultiEditActivity {

    private NoticeInfoActivityCustom dataBinding;
    private int id;
    private Bundle bundle;
    private TiaogangInfoBean items;
    private int who;
    private int status;

    @Override
    public void initBinding() {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_notice_info);
        initTopTitleBar(View.VISIBLE, "调岗申请详情");
        id = getIntent().getIntExtra(Config.ID, 0);
        who = getIntent().getIntExtra(Config.WHO, 0);
        status = getIntent().getIntExtra(Config.STATUS, 0);
        initTitle();
    }

    private void initTitle() {
        if (who >= 1 && status == 0) {
            initTopRightText("提交", v -> {
                if (checkParam()) {
                    OkGo.<HttpResult>post(HttpConfig.BASE_URL + HttpConfig.TIAOGANG_CHECK)
                            .params("changePostId", items.getId())
                            .params("userGuid", user.getGuid())
                            .params("message", dealText(dataBinding.etRemark.getText().toString()))
                            .params("type", dataBinding.rbAgree.isChecked() ? 1 : 2)
                            .execute(new DialogCallBack<HttpResult>(this) {
                                @Override
                                public void onSuccess(Response<HttpResult> response) {
                                    HttpResult httpResult = response.body();
                                    ToastUtil.showShort(httpResult.getMsg());
                                    if (httpResult.getStatus() == 200) {
                                        setResult(Config.DELETE);
                                        finish();
                                    }
                                }
                            });
                } else {
                    ToastUtil.showLong("真残忍，能否给个理由先？");
                }
            });
        }
        dataBinding.rbReject.setOnCheckedChangeListener((buttonView, isChecked) -> {
            if (isChecked) {
                dataBinding.rbAgree.setChecked(!isChecked);
            }
        });
        dataBinding.rbAgree.setOnCheckedChangeListener((buttonView, isChecked) -> {
            if (isChecked) {
                dataBinding.rbReject.setChecked(!isChecked);
            }
        });
    }

    private boolean checkParam() {
        return !(dataBinding.rbReject.isChecked() && TextUtils.isEmpty(dataBinding.etRemark.getText()));
    }

    @Override
    protected RecyclerView initRecyclerView() {
        return dataBinding.recyclerView;
    }

    @Override
    public void itemClick(int p) {

        switch (slimAdapterEx.getDataItem(p).getPosition()) {
            case 0:
                if (!TextUtils.isEmpty(items.getCreater().trim())) {
                    bundle = new Bundle();
                    bundle.putString(Config.GUID, items.getCreater());
                    startActivity(UserCardActivity.class, bundle);
                }
                break;
            case 1:
                if (!TextUtils.isEmpty(items.getChanger().trim())) {
                    bundle = new Bundle();
                    bundle.putString(Config.GUID, items.getChanger());
                    startActivity(UserCardActivity.class, bundle);
                }
                break;
            case 6:
                bundle = new Bundle();
                bundle.putString(Config.TITLE, "原因");
                bundle.putString(Config.CONTENT, items.getReason());
                startActivity(TextActivity.class, bundle);
                break;
            case 7:
                if (!TextUtils.isEmpty(items.getNextChecker().trim())) {
                    bundle = new Bundle();
                    bundle.putString(Config.GUID, items.getNextChecker());
                    startActivity(UserCardActivity.class, bundle);
                }
                break;
        }
    }

    @Override
    protected void loadNetData() {
        OkGo.<HttpResult<TiaogangInfoBean>>post(HttpConfig.BASE_URL + HttpConfig.TIAOGANG_INFO)
                .params("changePostId", id)
                .execute(new DialogCallBack<HttpResult<TiaogangInfoBean>>(TiaogangInfoActivity.this) {
                    @Override
                    public void onSuccess(Response<HttpResult<TiaogangInfoBean>> response) {
                        HttpResult<TiaogangInfoBean> httpResult = response.body();
                        ToastUtil.showShort(httpResult.getMsg());
                        if (httpResult.getStatus() == 200) {
                            items = httpResult.getItems();
                            datas.add(new SlimEditEnterBean("申请人", items.getCreaterStaffName(), 0, true, ""));
                            datas.add(new SlimEditEnterBean("要调岗的人", items.getChangerStaffName(), 1, true, ""));
                            datas.add(new SlimEditHintBean("现部门职位", items.getChangerDepartment() + "/" + items.getChangerPost(), 2));
                            datas.add(new SlimEditHintBean("要调部门职位", items.getChangeDepartment() + "/" + items.getChangePost(), 3));
                            datas.add(new SlimEditHintBean("申请薪资", items.getSalary(), 4));
                            datas.add(new SlimEditHintBean("调岗生效时间", items.getImplementTime(), 5));
                            datas.add(new SlimEditEnterBean("调岗原因", items.getReason(), 6, true, "原因无"));
                            if (!TextUtils.isEmpty(items.getNextChecker())) {
                                datas.add(new SlimEditEnterBean("下一审批人", items.getNextCheckerName(), 7, true, ""));
                            }
                            if (who >= 1&&status==0) {
                                dataBinding.llDownRoot.setVisibility(View.VISIBLE);
                            }
                            slimAdapterEx.updateData(datas);
                        }
                    }
                });
    }

    @Override
    protected void setEditBeans() {
        isSave = false;
        isRead = false;

    }
}
